package com.evaluation.kexingcp.psycho.evaluation.sys.tenant

import com.evaluation.kexingcp.common.core.util.R
import com.evaluation.kexingcp.common.security.annotation.Inner
import jakarta.annotation.Nonnull
import jakarta.servlet.http.HttpServletRequest
import jakarta.validation.Valid
import jakarta.validation.constraints.NotEmpty
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.*


data class TenantRequest(
    @Nonnull @NotEmpty val orgName: String? = null,
    @Nonnull @NotEmpty val mobile: String? = null,
    @Nonnull @NotEmpty val validateCode: String? = null,
    @Nonnull @NotEmpty val orgType: String? = null,
    @Nonnull @NotEmpty val email: String? = null,
)


data class WeChatPaymentCallbackResponse(val code: String, val message: String)
class SmsPaymentReq(val num: Int? = null)

data class BugVIPReq (
    @Nonnull @NotEmpty val tenantId: String? = null,
    @Nonnull @NotEmpty val goodsId: String? = null,
    val username: String? = null
)

@RestController
@RequestMapping("/tenants")
open class PesTenantsController(
    private val pesTenantService: PesTenantService
) {

    @PostMapping
    @Inner(false)
    open fun order(@Valid @RequestBody tenantRequest: TenantRequest, httpServletRequest: HttpServletRequest)
    : R<TenantResp> {
        return pesTenantService.createTenant(tenantRequest, httpServletRequest).fold(
            ifLeft = { err ->
                R.failed(err.message)
            },
            ifRight = { resp ->
                R.ok(resp, "创建成功")
            }
        )
    }

    @PostMapping("/buySms")
    open fun orderSms(@RequestBody smsPaymentReq: SmsPaymentReq, httpServletRequest: HttpServletRequest)
            : R<TenantResp> {
        return pesTenantService.buySms(smsPaymentReq, httpServletRequest).fold(
            ifLeft = { err ->
                R.failed(err.message)
            },
            ifRight = { resp ->
                R.ok(resp, "创建成功")
            }
        )
    }

    @PostMapping("/buyVIP")
    @Inner(false)
    open fun orderVIP(@RequestBody bugVIPReq: BugVIPReq, httpServletRequest: HttpServletRequest)
            : R<TenantResp> {
        return pesTenantService.buyVIP(bugVIPReq, httpServletRequest).fold(
            ifLeft = { err ->
                R.failed(err.message)
            },
            ifRight = { resp ->
                R.ok(resp, "创建成功")
            }
        )
    }

    @GetMapping("/orderStatus/{orderId}")
    @Inner(false)
    open fun orderStatus(@PathVariable orderId: String) : R<String> {
        return pesTenantService.orderStatus(orderId).fold(
            ifLeft = { err ->
                R.failed(err.message)
            },
            ifRight = { resp ->
                R.ok(resp)
            }
        )
    }

    // 微信支付成功回调
    @PostMapping("/{paymentMethod}/paymentCallback")
    @Transactional
    @Inner(false)
    open fun paymentCallback(request: HttpServletRequest,@PathVariable paymentMethod: String)
    : WeChatPaymentCallbackResponse
    {
        return pesTenantService.callback(request).fold(
            ifLeft = { err ->
                WeChatPaymentCallbackResponse("FAIL", err.message)
            },
            ifRight = {
                WeChatPaymentCallbackResponse("SUCCESS", "")
            }
        )
    }
}